package com.appfront;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.text.InputFilter;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Spinner;

import com.appfront.debug.App;
import com.auxtools.LINE;
import com.uicontrol.InputFilterMinMax;

import java.util.ArrayList;
import java.util.List;

import lecho.lib.hellocharts.gesture.ContainerScrollType;
import lecho.lib.hellocharts.model.Axis;
import lecho.lib.hellocharts.model.Line;
import lecho.lib.hellocharts.model.LineChartData;
import lecho.lib.hellocharts.model.Viewport;
import lecho.lib.hellocharts.view.LineChartView;

public class SetupActivity extends Activity {

    EditText editText_planName;
    EditText editText_group;
    EditText editText_freqs;
    EditText editText_groupTime;
    EditText editText_freqsTime;
    EditText editText_peakForceTime;
    Button button_saver;
    Button button_cancel;
    RadioButton radioButton_isStartN;
    RadioButton radioButton_balanceN;
    EditText editText_startForceN;
    /* 弹力带控件 */
    Spinner     spinner_lines;
    CheckBox    checkBox_lines;
    LineChartView chart_line;
    private LineChartData lineChartData = new LineChartData();
    private List<Line> chartLines = new ArrayList<Line>();
    LINE line_peach;
    LINE line_orange;
    LINE line_lime;
    LINE line_blueberry;
    LINE line_plum;
    LINE line_gray;
    LINE[] line_colors = new LINE[]{line_peach, line_orange, line_lime, line_blueberry, line_plum, line_gray};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setup);

        editText_planName = (EditText) findViewById(R.id.setup_input_planName);
        editText_group = (EditText) findViewById(R.id.setup_input_group);
        editText_freqs = (EditText) findViewById(R.id.setup_input_freqs);
        editText_groupTime = (EditText) findViewById(R.id.setup_input_groupTime);
        editText_freqsTime = (EditText) findViewById(R.id.setup_input_freqsTime);
        editText_peakForceTime = (EditText) findViewById(R.id.setup_input_peakForceTime);
        radioButton_balanceN = (RadioButton) findViewById(R.id.setup_balanceN);
        radioButton_isStartN = (RadioButton) findViewById(R.id.setup_startForceN);
        editText_startForceN = (EditText) findViewById(R.id.setup_input_startForce);
        editText_planName.setText(TestproSetup.getCurPlanName());
        editText_group.setText(Integer.toString(TestproSetup.getCurGroup()));
        editText_freqs.setText(Integer.toString(TestproSetup.getCurFreqs()));
        editText_groupTime.setText(Integer.toString(TestproSetup.getCurGroupTime()));
        editText_freqsTime.setText(Integer.toString(TestproSetup.getCurFreqsTime()));
        editText_peakForceTime.setText(Integer.toString(TestproSetup.getCurPeakForceTime()));
        radioButton_balanceN.setChecked(!TestproSetup.getIsStartForce());
        radioButton_isStartN.setChecked(TestproSetup.getIsStartForce());
        editText_startForceN.setText(Integer.toString(TestproSetup.getStartForceN()));
        editText_group.setFilters(new InputFilter[]{new InputFilterMinMax(1, 9)});
        editText_freqs.setFilters(new InputFilter[]{new InputFilterMinMax(1, 99)});
        editText_groupTime.setFilters(new InputFilter[]{new InputFilterMinMax(10, 600)});
        editText_freqsTime.setFilters(new InputFilter[]{new InputFilterMinMax(10, 600)});
        editText_peakForceTime.setFilters(new InputFilter[]{new InputFilterMinMax(1,600)});
       // editText_startForceN.setFilters(new InputFilter[]{new InputFilterMinMax(10,500)});
        button_saver   = (Button) findViewById(R.id.setup_button_save);
        button_cancel  = (Button) findViewById(R.id.setup_button_cancel);
        /* 弹力带参数 */
        spinner_lines = (Spinner) findViewById(R.id.setup_spinner_lines);
        checkBox_lines = (CheckBox) findViewById(R.id.setup_showAllLines);
        chart_line = (LineChartView) findViewById(R.id.setup_chart);     //折线图
        List<String> strList_group = new ArrayList<String>();
        for(int i=0; i<6; i++)
            strList_group.add(TestproSetup.lines_str[i]);
        ArrayAdapter<String> adapter_group = new ArrayAdapter<String>(this, R.layout.spinner_display_style, R.id.txtvwSpinner, strList_group);
        adapter_group.setDropDownViewResource(R.layout.spinner_dropdown_style);
        spinner_lines.setAdapter(adapter_group);
        spinner_lines.setSelection(TestproSetup.getCurLines()-1);

        initLineChart();
        initLineShow();

        spinner_lines.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                initLineShow();
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        checkBox_lines.setChecked(true);
        checkBox_lines.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                initLineShow();
            }
        });

        button_saver.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TestproSetup.setCurPlanName(editText_planName.getText().toString());
                TestproSetup.setCurGroup(Integer.parseInt(editText_group.getText().toString()));
                TestproSetup.setCurFreqs(Integer.parseInt(editText_freqs.getText().toString()));
                TestproSetup.setCurGroupTime(Integer.parseInt(editText_groupTime.getText().toString()));
                TestproSetup.setCurFreqsTime(Integer.parseInt(editText_freqsTime.getText().toString()));
                TestproSetup.setCurPeakForceTime(Integer.parseInt(editText_peakForceTime.getText().toString()));
                TestproSetup.setCurLines(spinner_lines.getSelectedItemPosition() + 1);
                TestproSetup.setIsStartForce(radioButton_isStartN.isChecked());
                TestproSetup.setStartForceN(Integer.parseInt(editText_startForceN.getText().toString()));
                TestproSetup.StoreSetup();  //保存配置文件
                App.toast("保存成功！", App.DEBUG_LEVEL.INFO);
                finish();
            }
        });
        button_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void initLineShow() {
        if(checkBox_lines.isChecked()){
            for(int i=0; i<6; i++)
                chartLines.add(line_colors[i].line);
        }else {
            for(int i=0; i<6; i++){
                chartLines.remove(line_colors[i].line);
            }
            chartLines.add(line_colors[spinner_lines.getSelectedItemPosition()].line);
        }
        chart_line.setLineChartData(lineChartData);

        Viewport viewport = chart_line.getMaximumViewport();
        viewport.left = 0;
        viewport.right = (float) 3.1;
        viewport.top = 28;
        viewport.bottom = 1;
        chart_line.setMaximumViewport(viewport);
        chart_line.setCurrentViewport(viewport);
    }

    private void initLineChart(){

        line_peach      =  new LINE(chartLines, Color.rgb(253,139,254), false, true);
        line_orange     = new LINE(chartLines, Color.rgb(0x6f, 0x42, 0x42), false, true);
        line_lime       = new LINE(chartLines, Color.GREEN, false, true);
        line_blueberry = new LINE(chartLines, Color.BLUE, false, true);
        line_plum       = new LINE(chartLines, Color.rgb(0x93, 0x70, 0xdb), false, true);
        line_gray       = new LINE(chartLines, Color.GRAY, false, true);
        lineChartData.setLines(chartLines);

        line_colors[0] = line_peach;
        line_colors[1] = line_orange;
        line_colors[2] = line_lime;
        line_colors[3] = line_blueberry;
        line_colors[4] = line_plum;
        line_colors[5] = line_gray;

        for(int i=0; i<12; i++){
            line_peach.add((float) TestproSetup.line_scale[i], (float) TestproSetup.line_peach[i]);
            line_orange.add((float) TestproSetup.line_scale[i], (float) TestproSetup.line_orange[i]);
            line_lime.add((float) TestproSetup.line_scale[i], (float) TestproSetup.line_green[i]);
            line_blueberry.add((float) TestproSetup.line_scale[i], (float) TestproSetup.line_blue[i]);
            line_plum.add((float) TestproSetup.line_scale[i], (float) TestproSetup.line_plum[i]);
            line_gray.add((float) TestproSetup.line_scale[i], (float) TestproSetup.line_gray[i]);
        }
        //坐标轴
        Axis axisX = new Axis();            //X轴
        axisX.setHasTiltedLabels(true);     //X坐标轴字体是斜的显示还是直的，true是斜的显示
        //axisX.setTextColor(Color.BLUE);    //设置字体颜色
        //axisX.setName("date");            //表格名称
        axisX.setTextSize(10);              //设置字体大小
        axisX.setMaxLabelChars(12);          //最多几个X轴坐标，意思就是你的缩放让X轴上数据的个数7<=x<=mAxisXValues.length
        //axisX.setValues(mAxisXValues);      //填充X轴的坐标名称
        lineChartData.setAxisXBottom(axisX);         //x 轴在底部
        //data.setAxisXTop(axisX);          //x 轴在顶部
        axisX.setHasLines(true);            //x 轴分割线

        // Y轴是根据数据的大小自动设置Y轴上限(在下面我会给出固定Y轴数据个数的解决方案)
        Axis axisY = new Axis();            //Y轴
        axisY.setName("");                  //y轴标注
        axisY.setTextSize(10);              //设置字体大小
        lineChartData.setAxisYLeft(axisY);           //Y轴设置在左边

        //设置行为属性，支持缩放、滑动以及平移
        chart_line.setInteractive(true);
        chart_line.setZoomEnabled(true);
        chart_line.setMaxZoom((float) 4);//最大方法比例
        chart_line.setContainerScrollEnabled(true, ContainerScrollType.HORIZONTAL);
        chart_line.setLineChartData(lineChartData);

        chart_line.setVisibility(View.VISIBLE);
    }
}
